package ntut.andengine.physics;

import java.util.ArrayList;
import java.util.List;

import ntut.andengine.events.UpdateCommand;
import ntut.andengine.game.GameLevel;

import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.extension.physics.box2d.PhysicsConnector;
import org.anddev.andengine.extension.physics.box2d.PhysicsFactory;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.bitmap.BitmapTexture;
import org.anddev.andengine.opengl.texture.bitmap.BitmapTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;

import android.util.Log;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.FixtureDef;

public class SwitchDevice extends PhysicalComponent {
	
	Sprite offSprite;
	boolean isOn = true;
	List<DeathLight> mDeatLightList;
	List<Body> contactBodyList;
	
	public List<DeathLight> getDeatLightList() {
		return mDeatLightList;
	}

	public void setDeatLightList(List<DeathLight> deatLightList) {
		this.mDeatLightList = deatLightList;
	}

	public SwitchDevice(GameLevel level, boolean isVertical) {
		super(level);
		
		contactBodyList = new ArrayList<Body>();
		
		this.mBitmapTexture = new BitmapTexture(128, 128, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		TextureRegion onTextureRegion = BitmapTextureRegionFactory.createFromAsset(this.mBitmapTexture, level, "on.png", 0, 0);
		TextureRegion offTextureRegion = BitmapTextureRegionFactory.createFromAsset(this.mBitmapTexture, level, "off.png", 0, 40);
		level.getEngine().getTextureManager().loadTexture(this.mBitmapTexture);	
		
		mSprite = new Sprite(0, 0, onTextureRegion);
		//mSprite.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE);
		offSprite = new Sprite(-100, -100, offTextureRegion);
		//offSprite.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE);
		if (isVertical)
        {
       	 	offSprite.setRotation(90);
        }

		FixtureDef fixtureDef = PhysicsFactory.createFixtureDef(1, 0, 0);
		fixtureDef.isSensor = true;
		mBody = PhysicsFactory.createBoxBody(level.getPhysicsWorld(), mSprite, BodyType.DynamicBody, fixtureDef);
		mBody.setUserData(new ContactEvent("SwitchDevice"){

			@Override
			public void beginContact(Body body, Vector2 Normal) {
				//Log.d("start", body.getUserData().toString());
				contactBodyList.add(body);
				if(contactBodyList.size() > 0){        
			         offSprite.setPosition(mSprite.getX(), mSprite.getY());
			         isOn = false;
				}
			}

			@Override
			public void endContact(Body body, Vector2 Normal) {
				Log.d("end", body.getUserData().toString());
				contactBodyList.remove(body);
			}});
		
		level.getScene().attachChild(mSprite);
		level.getScene().attachChild(offSprite);
		level.getPhysicsWorld().registerPhysicsConnector(new PhysicsConnector(mSprite, mBody, true, true));
		
		level.addUpdateCommand(new UpdateCommand(){

			@Override
			public void updateCommand(float pSecondsElapsed) {
				OnOff(pSecondsElapsed);
				turnOn();
			}
			
		});
	}

	private void turnOn() {
				if(contactBodyList.size() == 0){
					offSprite.setPosition(-100, -100);
					isOn = true;
				}
			}
	
	private void OnOff(float time){
        if (isOn){
            for (DeathLight deathLight : mDeatLightList){
            	deathLight.mBody.setActive(true);
            	deathLight.getParticleSystem().setParticlesSpawnEnabled(true);
            }
            
        }else{
        	for (DeathLight deathLight : mDeatLightList){
            	deathLight.mBody.setActive(false);
            	deathLight.getParticleSystem().setParticlesSpawnEnabled(false);
            }
        }
    }

}
